package io.imp.modules.tasks.unionqrc.task;

import io.imp.modules.job.task.ITask;
import io.imp.modules.tasks.unionqrc.entity.*;
import io.imp.modules.tasks.unionqrc.service.BaseService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.*;

import static io.imp.modules.tasks.unionqrc.task.ItcUnionqrcPackFile.*;

/**
 * 功能描述:
 * <日终补漏 生成数据文件>
 *
 * @ClassName: ItcUnionqrcPackFile
 * @Date: 2019年05月17日 13:55:46
 *
 * @Copyright: 2019 Inc. All rights reserved. 注意: 本内容仅限于内部传阅,禁止外泄以及用于其他的商业目
 */
@Component("ItcUnionqrcPackFileLeakFilling")
public class ItcUnionqrcPackFileLeakFilling extends BaseService implements ITask  {
    private Logger logger = LoggerFactory.getLogger(getClass());


    /**
     * 废弃备用
     */
    public void getsource() {

        logger.info("进入查漏补缺方法");
        List<ItcsUnionpayTxnJnlPart> itcsUnionpayTxnJnlParts = new LinkedList<ItcsUnionpayTxnJnlPart>();
        //结果集合
        Map<String,String> stationMap = getItcsStations();
        LinkedList<String> resultList = new LinkedList<String>();
        Calendar calendar = Calendar.getInstance();
        Date date = new  Date();
        calendar.setTime(date);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:00");
        String creatTime = sdf.format(calendar.getTime());
        calendar.add(Calendar.DATE,-1);
        //获取当前天数
        SimpleDateFormat sfdForDay = new SimpleDateFormat("yyyy-MM-dd");
        String nowTime = sfdForDay.format(calendar.getTime());
        List<Object[]> list = itcsUnionpayTxnJnlService.LeakFilling(nowTime);
        logger.info("今天漏了 {} 条数据",list.size());
        if (list.size()==0){
            return;
        }else {
            for (Object[] o: list) {
                String orderId = o[1].toString();
                logger.info("漏掉的订单号：{}",orderId);
                ItcsUnionpayTxnJnl itcsUnionpayTxnJnl = itcsUnionpayTxnJnlService.getItcsUnionpayTxnJnlWithOne(new String[]{"orderid"},new Object[]{orderId});
                ItcsUnionpayTxnJnlPart itcsUnionpayTxnJnlPart = new ItcsUnionpayTxnJnlPart();
                BeanUtils.copyProperties(itcsUnionpayTxnJnl,itcsUnionpayTxnJnlPart);
                itcsUnionpayTxnJnlPart.setLastdatetime(creatTime);
                itcsUnionpayTxnJnlParts.add(itcsUnionpayTxnJnlPart);
                ItcsTransOrderJnl itcsTransOrderJnl = itcsTransOrderJnlService.getByOrderNo(orderId);

                String terminalno = itcsTransOrderJnl.getTerminalno();
                String estationid = itcsTransOrderJnl.getEstationid();
                if (terminalno == null || terminalno.equals("")){
                    terminalno = "未找到终端号";
                }
                if (estationid == null || estationid.equals("")){
                    estationid = "G0000000000000";
                }
                resultList.add(getStringSource(itcsUnionpayTxnJnl,itcsTransOrderJnl,stationMap,terminalno,estationid));
            }
        }
        String path = sysDictService.getDictValue("unionqrc","filePath");
        String prefix = "GS";
        String suffix = ".txt";
        if (resultList.size()==0){
            return;
        }
        writeFile(path,prefix,suffix,resultList);

        itcsUnionpayTxnJnlPartService.saveList(itcsUnionpayTxnJnlParts);
    }

    /**
     * 查漏补缺
     * @param params  参数，多参数使用JSON数据
     */
    @Override
    public void run(String params){
        logger.info("进入银联日终文件补漏方法");
        //站点结果集合
        Map<String,String> stationMap = getItcsStations();
        //String结果集合
        LinkedList<String> resultList = new LinkedList<String>();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,-1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String nowTime = sdf.format(calendar.getTime());
        //渠道订单集合 flag为0的
        List<ItcsUnionpayTxnJnl> ObjTxnList = itcsUnionpayTxnJnlService.LeakFillingTxn(nowTime);
        //渠道撤销集合 flag为0的
        List<ItcsUnionpayCancelJnl> ObjCancelList = itcsUnionpayCancelJnlService.LeakFillingCancel(nowTime);
        //渠道退款集合 flag为0的
        List<ItcsUnionpayRefundJnl> ObjRefundList = itcsUnionpayRefundJnlService.LeakFillingRefund(nowTime);
        //渠道冲正集合 flag为0的
        List<ItcsUnionpayReverseJnl> ObjReverseList = itcsUnionpayReverseJnlService.LeakFillingReverse(nowTime);

        logger.info("订单漏传集合，共{}条",ObjTxnList.size());
        logger.info("撤销漏传集合，共{}条",ObjCancelList.size());
        logger.info("退款漏传集合，共{}条",ObjRefundList.size());
        logger.info("冲正漏传集合，共{}条",ObjReverseList.size());

        //渠道订单号集合
        List<String> orderNoList = new LinkedList<String>();
        for (ItcsUnionpayTxnJnl o: ObjTxnList) {
            orderNoList.add(o.getOrderid());
        }

        //所有漏掉的实体
        HashMap<String,List> hashMap = new HashMap<>();
        hashMap.put(ItcsUnionpayTxnJnls,ObjTxnList);
        hashMap.put(ItcsUnionpayCancelJnls,ObjCancelList);
        hashMap.put(ItcsUnionpayRefundJnls,ObjRefundList);
        hashMap.put(ItcsUnionpayReverseJnls,ObjReverseList);

        //漏掉的渠道订单对应的流水集合
        Map<String,ItcsTransOrderJnl> itcstransOrderJnlMap = new HashMap<String, ItcsTransOrderJnl>();
        if (orderNoList.size()!=0){
            List<ItcsTransOrderJnl> orderJnls = itcsTransOrderJnlService.getByOrderno(orderNoList);
            for (ItcsTransOrderJnl I: orderJnls) {
                itcstransOrderJnlMap.put(I.getOrderno(),I);
            }
        }
        logger.info("流水集合，共{}条",itcstransOrderJnlMap.size());

        //获取需要打印的字符串
        String result = getStringSource(hashMap,itcstransOrderJnlMap,stationMap);
        if (StringUtils.isBlank(result)){
            logger.info("没有需要打印的数据");
            return;
        }
        resultList.add(result);
        System.out.println(result);
        String path = sysDictService.getDictValue("unionqrc","filePath");
        String prefix = "GS";
        String suffix = ".txt";
        if (StringUtils.isBlank(result)){
            return;
        }
        writeFile(path,nowTime,prefix,suffix,resultList);

        logger.info("打印完毕");

    }
}
